import { HttpException, HttpStatus } from "@nestjs/common";
import { Min } from "class-validator";
import { Delivery } from "src/modules/delivery/delivery.entity";
import { Order } from "src/modules/order/order.entity";
import { CustomizeStatus } from "src/utils/status";
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class DeliveryItem {
  @PrimaryGeneratedColumn()
  id: string;

  @Column("int")
  @Min(1)
  quantity: number;

  @ManyToOne(() => Delivery, (delivery) => delivery.deliveryItems, {
    nullable: false,
  })
  delivery: Delivery;

  @ManyToOne(() => Order, (order) => order.deliveryItems, { nullable: false })
  order: Order;

  set newQuantity(value: number) {
    this.quantity = value;
    if (this.quantity < 1) {
      throw new HttpException(
        {
          message: "数量不足",
          target: this,
          statusCode: CustomizeStatus.LACKING,
        },
        HttpStatus.FORBIDDEN
      );
    }
  }
}
